﻿using System;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Xml;

namespace WebsiteLoadTool
{
    [Serializable]
    public class WebsiteLoadResult : INotifyPropertyChanged
    {
        private Guid id;
        private ResultStatusEnum status;
        private DateTime startDateTime;
        private DateTime? endDateTime;
        private TimeSpan duration;
        private string message;

        public Guid ID
        {
            get { return id; }
            set
            {
                if (!Guid.Equals(id, value))
                {
                    id = value;
                    RaisePropertyChanged("ID");
                }
            }
        }

        public ResultStatusEnum Status
        {
            get { return status; }
            set
            {
                if (!ResultStatusEnum.Equals(status, value))
                {
                    status = value;
                    RaisePropertyChanged("Status");
                }
            }
        }

        public DateTime StartDateTime
        {
            get { return startDateTime; }
            set
            {
                if (!DateTime.Equals(startDateTime, value))
                {
                    startDateTime = value;
                    RaisePropertyChanged("StartDateTime");
                }
            }
        }

        public DateTime? EndDateTime
        {
            get { return endDateTime; }
            set
            {
                if (!Nullable<DateTime>.Equals(endDateTime, value))
                {
                    endDateTime = value;
                    RaisePropertyChanged("EndDateTime");
                }
            }
        }

        [XmlIgnore]
        public TimeSpan Duration
        {
            get { return duration; }
            set
            {
                if (!TimeSpan.Equals(duration, value))
                {
                    duration = value;
                    RaisePropertyChanged("Duration");
                }
            }
        }

        public string Message
        {
            get { return message; }
            set
            {
                if (!string.Equals(message, value))
                {
                    message = value;
                    RaisePropertyChanged("Message");
                }
            }
        }

        // XmlSerializer does not support TimeSpan, so use this property for serialization instead.
        [Browsable(false)]
        [XmlElement(DataType = "duration", ElementName = "Duration")]
        public string TimeSpanDuration
        {
            get { return XmlConvert.ToString(Duration); }
            set { Duration = string.IsNullOrEmpty(value) ? TimeSpan.Zero : XmlConvert.ToTimeSpan(value); }
        }

        #region INotifyPropertyChanged
        private event PropertyChangedEventHandler propertyChanged;

        public event PropertyChangedEventHandler PropertyChanged
        {
            add { propertyChanged += value; }
            remove { propertyChanged -= value; }
        }

        private void RaisePropertyChanged(string propertyName)
        {
            if (propertyChanged != null)
            {
                propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region public overrides
        public override bool Equals(object obj)
        {
            if (obj == null || !(obj is WebsiteLoadResult)) return false;
            if (Guid.Equals(this.ID, Guid.Empty)) return base.Equals(obj);
            return Guid.Equals(this.ID, ((WebsiteLoadResult)obj).ID);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override string ToString()
        {
            return Status.ToString();
        }

        #endregion
    }
}
